package main

import (
	"bytes"
	"database/sql"
	"fmt"
	"fyne.io/fyne"
	"fyne.io/fyne/app"
	"fyne.io/fyne/container"
	"fyne.io/fyne/widget"
	"github.com/flopp/go-findfont"
	_ "github.com/go-sql-driver/mysql"
	_ "github.com/godror/godror"
	"html/template"
	"io/ioutil"
	"os"
	"strings"
	"totest/src/components"
	"totest/src/pojo"
)

/**
设置中文下的使用字体，如果不进行设置则会导致中文乱码
*/
var dataSource = new(pojo.DataSource)
var db *sql.DB
var column = make([]pojo.JiuColumn, 0)

func init() {

	fontPaths := findfont.List()
	for _, path := range fontPaths {
		fmt.Println(path)
		if strings.Contains(path, "Deng.ttf") {
			os.Setenv("FYNE_FONT", path)
			break
		}
	}
}
func createFile() {
	_, error := os.Stat(pojo.SOURCE_FILE)
	byte := make([]byte, 10)
	if error != nil {
		if os.IsNotExist(error) {
			fmt.Println("文件不存在进行" + pojo.SOURCE_FILE + "创建")
			ioutil.WriteFile(pojo.TEMPLATE_FILE, byte, 0666)
			ioutil.WriteFile(pojo.SOURCE_FILE, byte, 0666)
		}
	}
}
func main() {
	createFile()
	a := app.New()
	w := a.NewWindow("小程序")
	w.Resize(fyne.Size{
		Width:  1000,
		Height: 600,
	})
	coonet := components.NewWindowOfCenterNoconent("连接", fyne.Size{
		Width:  400,
		Height: 200},
	)
	form, dataSource := components.MakeFormTab(dataSource, coonet)
	coonet.SetContent(form)
	template := widget.NewMultiLineEntry()
	templateFile, ok := ioutil.ReadFile(pojo.TEMPLATE_FILE)
	if ok == nil {
		template.SetText(bytes.NewBuffer(templateFile).String())
	}
	templateView := widget.NewMultiLineEntry()
	templateView.Wrapping, template.Wrapping = fyne.TextWrapWord, fyne.TextWrapWord
	leftSplit := container.NewHSplit(template, templateView)
	mainMenu := fyne.NewMainMenu(
		fyne.NewMenu("更多",
			fyne.NewMenuItem("关于", func() {
				components.NewWindowOfFixedAndCenter(
					"关于", widget.NewLabel("使用golang自带的template语法进行渲染\n模板中.list\n有以下几个属性\nColumn     //列名\nColumnType //类型\nCamelCase  //转驼峰"))
			}),
			fyne.NewMenuItemSeparator(),
			fyne.NewMenuItem("连接", func() {
				coonet.Show()
			}),
			fyne.NewMenuItemSeparator(),
			fyne.NewMenuItem("渲染", func() {
				templateView.SetText(templateUpdate(template.Text))
				ioutil.WriteFile(pojo.TEMPLATE_FILE, bytes.NewBufferString(template.Text).Bytes(), 0666)
			}),
		))
	inputSql := widget.NewMultiLineEntry()
	inputSql.Wrapping = fyne.TextWrapBreak
	createTable := widget.NewTable(
		func() (int, int) {
			if len(column) == 0 {
				return 1, 4
			}
			return len(column), 4
		},
		func() fyne.CanvasObject {
			return widget.NewLabel("")
		},
		func(id widget.TableCellID, object fyne.CanvasObject) {
			lab := object.(*widget.Label)
			switch id.Col {
			case 0:
				if id.Row == 0 {
					lab.SetText("编号")

				} else {
					lab.SetText(fmt.Sprintf("%d", id.Row))
				}

			case 1:
				if id.Row == 0 {
					lab.SetText("名称")
				} else {
					if len(column) > 0 {
						lab.SetText(column[id.Row].CamelCase)
					}
				}

			case 2:
				if id.Row == 0 {
					lab.SetText("逻辑名")
				} else {
					if len(column) > 0 {
						lab.SetText(column[id.Row].Column)
					}
				}
			case 3:
				if id.Row == 0 {
					lab.SetText("数据类型")
				} else {
					if len(column) > 0 {
						lab.SetText(column[id.Row].ColumnType)
					}
				}
			}
		})
	createTable.SetColumnWidth(0, 34)
	createTable.SetColumnWidth(1, 122)
	createTable.SetColumnWidth(2, 122)
	createTable.SetColumnWidth(3, 122)
	inputCartSql := container.NewVBox(
		widget.NewLabel("输入查询sql"),
		inputSql,
		widget.NewButton("运行sql", func() {
			fmt.Printf("%v%p\n", "main", dataSource)
			column = dataSource.GetColumn(inputSql.Text, &w)
			createTable.Refresh()

		}),
	)
	//inputCartSql.Add(createTable)
	leadSplit := container.NewHSplit(container.NewVSplit(
		inputCartSql,
		createTable), leftSplit)
	leadSplit.Offset = 0.3
	w.SetContent(leadSplit)
	w.SetMainMenu(mainMenu)
	w.CenterOnScreen()
	w.SetMaster()
	w.ShowAndRun()
	os.Unsetenv("FYNE_FONT")
}
func templateUpdate(tp string) string {
	var tpV = template.New("test")
	defer func() {
		err := recover()
		if err != nil {
			fmt.Println(err)
		}
	}()
	example := make(map[string]interface{}, 20)
	example["list"] = column
	str := bytes.NewBuffer(make([]byte, 0))
	tmp, ok := tpV.Parse(tp)
	if ok != nil {
		fmt.Println("create template failed, err:", ok)
		return ""
	}
	tmp.Execute(str, example)

	return str.String()
}
